//
//  ViewController3.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/4/13.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources

class RxViewController3: BaseViewController {
    let disposeBag = DisposeBag()
    var tableView: UITableView!
    var dataS = GithubData()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "分组显示的Table"
        self.view.backgroundColor = UIColor.orange
        tableView = UITableView(frame: self.view.bounds, style: .plain)
        tableView.register(SectionTableCell.self, forCellReuseIdentifier: SectionTableCell.description())
        self.view.addSubview(tableView)


        // 总结 : 开发
        // cell 数值 -- 配置  --- 闭包 -- 函数式
        // table.reloadData  --- 响应式

        let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, SectionDataModel>>(configureCell: { (ds, tb, indexPath, model) -> UITableViewCell in

            let cell = tb.dequeueReusableCell(withIdentifier: SectionTableCell.description(), for: indexPath) as? SectionTableCell
            cell?.setValue(model: model)
            return cell!

        }, titleForHeaderInSection: { (ds, index) -> String? in
            return ds.sectionModels[index].model //[self.dataArray[indexPath.section] title]//self.dataArray[indexPath.section][indexPath.row]
        })

        tableView.rx.itemSelected.subscribe(onNext: { indexpath in
            print("点击了 \(indexpath.row)")
        }).disposed(by: disposeBag)

        tableView.rx.modelSelected(SectionDataModel.self).subscribe(onNext: { (model) in
            print(model.name)
        }).disposed(by: disposeBag)

        dataS.githubData.asDriver(onErrorJustReturn: [])
            .drive(tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(pushSearchVC))
    }

    @objc func pushSearchVC() {
        self.navigationController?.pushViewController(TestViewController(), animated: true)
    }
}
